package cn.agiledata.bank.common.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.account.service.IAccountConst;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.OperSubacct;
import cn.agiledata.bank.hibernateBean.Operator;
import cn.agiledata.bank.hibernateBean.SubAccount;

public class PermissionService {

    public boolean hasPermission(HttpServletRequest request, String type) {

        if (isCorpAdmin(request)) {
            return false;
        }

        if (isCorpOperator(request)) {
            AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
            Operator oa = user.getOperatorByIdInstance(user.getOperatorCode()
                    .toString());
            Set s = oa.getOperSubaccts();
            Iterator iter = s.iterator();
            while (iter.hasNext()) {
                OperSubacct os = (OperSubacct) iter.next();
                Long listSeq = os.getId().getSysbook().getListSeq();
                if (listSeq.toString().equals(type)) {
                    return true;
                }
            }

            return false;
        } else {
            return true;
        }
    }

    public static PermissionService getPermissionService() {
        return new PermissionService();
    }

    public List getSubAccounts(HttpServletRequest request,
            String permissionType, SubAccount defaultSubAccount) {
        List c = new ArrayList();
        Long subId = null;
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        if(user.getOperatorCode()==null){
            return c; // 非操作员则返回子账户集合
        }
        Operator oa = user.getOperatorByIdInstance(user.getOperatorCode()
                .toString());
        Set s = oa.getOperSubaccts();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            OperSubacct os = (OperSubacct) iter.next();
            Long listSeq = os.getId().getSysbook().getListSeq();
            if (listSeq.toString().equals(permissionType)) {
                SubAccount sub = os.getId().getSubAccount();
                if (sub.getId().equals(defaultSubAccount.getId())) {
                    c.add(sub);
                    subId = sub.getId();
                }
            }
        }
        iter = s.iterator();
        while (iter.hasNext()) {
            OperSubacct os = (OperSubacct) iter.next();
            Long listSeq = os.getId().getSysbook().getListSeq();
            if (listSeq.toString().equals(permissionType)) {
                SubAccount sub = os.getId().getSubAccount();
                if (sub.getId() != subId && sub.getValidFlag().equals("Y")) {
                    c.add(sub);
                }
            }
        }
        return c;
    }

    public boolean isCorpAdmin(HttpServletRequest request) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        return IAccountConst.USERTYPE_CORP_ADMIN.equals(user.getUserType());
    }

    public boolean isCorpOperator(HttpServletRequest request) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        return IAccountConst.USERTYPE_CORP_OPERATOR.equals(user.getUserType());
    }

    public boolean isPerson(HttpServletRequest request) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        return IAccountConst.USERTYPE_PERSONAL.equals(user.getUserType());
    }
}
